from datetime import timedelta
import datetime
from dateutil.relativedelta import relativedelta
from predict_tool import *


def calculatePosition(list1, list2):
    list1 = list(set(list1))
    list2 = list(set(list2))
    commonList = [value for value in list1 if value in list2 and value is not None]
    uniqueList = [value for value in list1 if value not in list2 and value is not None]
    listRet = []
    for key in list2:
        if key in commonList:
            listRet.append(key)
    for key in uniqueList:
        listRet.append(key)
    return listRet


if __name__ == '__main__':
    fileNames = os.listdir('./data/provider')
    providerDic = {}
    prefix = './data/provider/'
    for fileName in fileNames:
        if fileName.startswith('.'):
            continue
        excelFile = pd.ExcelFile(prefix + fileName)
        sheetNames = excelFile.sheet_names
        # 临时cabinet柜子
        tempDic = {}
        for sheetName in sheetNames:
            received = False  # 这箱货柜到货了没
            cabinetLoading = False  # 这个柜子装柜了没
            sheetPurchase = pd.read_excel(prefix + fileName, header=None, sheet_name=sheetName)
            columnPre = sheetPurchase.columns
            names = ['sku', 'qty']
            fillSheetNameNewColumnsPadding(names, columnPre)
            # 先拿第一行数据
            firstRow = sheetPurchase.iloc[0]
            origin = str(firstRow[0])  # 供应商
            cabinet = firstRow[1]  # 柜号
            actuallyTime = ''
            if len(firstRow) >= 3:
                eta = firstRow[2]  # 到货时间
                # 有eta了代表肯定装柜了
                cabinetLoading = True
                if len(firstRow) >= 4:
                    actuallyTime = firstRow[3]
            # 没eta就是三个月后的今天
            else:
                eta = datetimeToTime(date.today() + relativedelta(months=3))
            sheetPurchase = sheetPurchase[2:]
            # 换columns名之前要先切片
            sheetPurchase.columns = names
            cabinetDicTemp = {}
            for cabinetIdx, cabinetRow in sheetPurchase.iterrows():
                cabinetDicTemp[cabinetRow['sku']] = cabinetRow['qty']
            if actuallyTime is not None and actuallyTime != '':
                received = True  # 已经到货
                cabinetDicTemp['eta'] = actuallyTime
            else:
                cabinetDicTemp['eta'] = eta
            # 是否到货 是否装柜
            cabinetDicTemp['received'] = received
            cabinetDicTemp['cabinetLoading'] = cabinetLoading
            tempDic[cabinet] = cabinetDicTemp
        providerDic[fileName] = tempDic
    print(providerDic)
